/* -----------------------------------------------------------------------------
 * PROJECT    : lwf
 * FILE       : ArithmaticsWorksheetAction.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * Mar 25, 2011  mycodetalks			       Created
 */
package net.lwf.web.struts.action.maths;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lwf.handler.IProcessHandler;
import lwf.handler.entities.RadioData;
import lwf.web.struts.action.constant.IActionConstant;
import net.lwf.handler.math.arithmatics.ArithmaticProcessHandler;
import net.lwf.jaxb.entity.StaticWorkSheet;
import net.lwf.jaxb.service.XMLDataService;
import net.lwf.jaxb.service.maths.worksheet.StaticWorkSheetService;
import net.lwf.web.struts.action.worksheet.StaticWorkSheetAction;
import net.lwf.web.struts.action.worksheet.StaticWorksheetActionForm;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 * Class description goes here
 */
public class ArithmaticsWorksheetAction extends StaticWorkSheetAction {
	
	public ActionForward chooseoperation(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		StaticWorksheetActionForm swaform = (StaticWorksheetActionForm) form;
		swaform.setRadioData(getOperationsData());
		return mapping.findForward("operations");
	} //chooseoperation
	
	public ActionForward home(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {

		String subject = "maths";
		String topic = request.getParameter("operation");
			
		StaticWorksheetActionForm swaform = (StaticWorksheetActionForm) form;
		swaform.setSubject(subject);
		swaform.setTopic(topic);
		try {
			StaticWorkSheetService service = new StaticWorkSheetService();
			List<StaticWorkSheet> worksheets = service.getUniqueWorkSheets(getGrade(), subject, topic);
			this.setPrintKey(worksheets);
			swaform.setWorksheets(worksheets);			
		}
		catch(Exception ex) {
			swaform.setRadioData(getOperationsData());
			swaform.setHasStaticWorksheets(false);
			String pdfLink = IActionConstant.ARITHMATICS_PDF_LINK + "&operation=" + getOperation();
			swaform.setDynamicPdfLink(pdfLink);
			return mapping.findForward("operations");			
		}

		
		return mapping.findForward("staticworksheets");
	} //home
	
	public IProcessHandler getHandler(HttpServletRequest request) {
		//return new ArithmaticProcessHandler(getGrade(), getOperation(request));
		return new ArithmaticProcessHandler(getGrade(), getOperation());
	}
	
	private List<RadioData> getOperationsData() {
		List<RadioData> radiodata = new ArrayList<RadioData>();
		XMLDataService service = new XMLDataService();
		List<String> operations = service.getOperations(Integer.parseInt(getGrade()));
		for(String data: operations) {
			RadioData radio = new RadioData(data, data);
			radiodata.add(radio);
		}
		return radiodata;
	}

}



